package com.witdata.mysql;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.zaxxer.hikari.HikariDataSource;

public class SqlCodegen {
    public static void main(String[] args) {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://web.yunnet.top:12306/witdata?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
        dataSource.setUsername("witdata");
        dataSource.setPassword("witdata");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig, IDialect.MYSQL);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle() {

        //从项目根目录启动
        String sourceDir = System.getProperty("user.dir") + "/witdata-mysql";

        //从witdata-mysql单独启动
        // String sourceDir = System.getProperty("user.dir");

        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        // 设置文件输出目录
        globalConfig.setSourceDir(sourceDir + "/src/main/java");

        //设置根包
        globalConfig.setBasePackage("com.witdata.mysql");

        //设置表前缀和只生成哪些表
        globalConfig.setTablePrefix("t_");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)
                .setClassPrefix("T")
                .setClassSuffix("")
                .setOverwriteEnable(true);
        globalConfig.setEntityPackage(globalConfig.getBasePackage() + ".domain.entity");

        // 设置生成service
        globalConfig.enableService()
                .setOverwriteEnable(true)
                .setClassSuffix("BaseService");
        globalConfig.setServicePackage(globalConfig.getBasePackage() + ".base.service");
        // 设置生成serviceImpl
        globalConfig.enableServiceImpl()
                .setOverwriteEnable(true)
                .setClassSuffix("BaseServiceImpl");
        globalConfig.setServiceImplPackage(globalConfig.getBasePackage() + ".base.service.impl");
        // 设置生成 mapperXml
        globalConfig.enableMapperXml()
                .setOverwriteEnable(true);
        globalConfig.setMapperXmlPath(sourceDir + "/src/main/resources/base/mapper");
        //设置生成 mapper
        globalConfig.enableMapper()
                .setOverwriteEnable(true)
                .setClassSuffix("BaseMapper");
        globalConfig.setMapperPackage(globalConfig.getBasePackage() + ".base.mapper");

        return globalConfig;
    }
}
